// ==========================================================================
// gin-sns自动生成业务逻辑层相关代码，只生成一次，按需修改,再次生成不会覆盖.
// date：2021-02-21 13:45:54 +0800 CST
// path: app/service/module/tag_service.go
// author：jon
// ==========================================================================
package module

import (
	"errors"
	"gin-sns/app/dao/module/tag"
	"gin-sns/app/model/constants"
	tagModel "gin-sns/app/model/module/tag"
	"strings"
	"time"

	"github.com/gin-gonic/gin"

	"gin-sns/app/ginframe/utils/convert"
	"gin-sns/app/ginframe/utils/page"
)

var TagService = newTagService()

func newTagService() *tagService {
	return &tagService{}
}

type tagService struct {
}

//根据主键查询数据
func (s *tagService) SelectRecordById(id int64) (*tagModel.Entity, error) {
	return tag.TagDao.SelectById(id)
}

//
func (s *tagService) SelectRecordsIn(idarr []int64) ([]tagModel.Entity, error) {

	list, err := tag.TagDao.FindIn("id", idarr)
	if list != nil && len(list) > 0 {
		err = errors.New("not found")
		list = nil
	}
	return list, err
}

//根据主键删除数据
func (s *tagService) DeleteRecordById(id int64) bool {
	return tag.TagDao.DeleteById(id)
}

//批量删除数据记录
func (s *tagService) DeleteRecordByIds(ids string) int64 {
	ida := convert.ToInt64Array(ids, ",")
	result, err := tag.TagDao.DeleteBatch(ida...)
	if err != nil {
		return 0
	}
	return result
}

//添加数据
func (s *tagService) AddSave(req *tagModel.AddReq, ctx *gin.Context) (int64, error) {
	var entity tagModel.Entity

	//entity.Id = req.Id
	entity.Title = req.Title
	entity.Status = req.Status
	entity.CreateTime = time.Now()
	//entity.CreateBy = ""

	//user := system.UserService.GetProfile(ctx)

	//if user != nil {
	//	entity.CreateBy = user.LoginName
	//}

	_, err := tag.TagDao.Insert(&entity)
	return entity.Id, err
}

//修改数据
func (s *tagService) EditSave(req *tagModel.EditReq, ctx *gin.Context) (int64, error) {
	entity := &tagModel.Entity{Id: req.Id}
	ok, err := tag.TagDao.FindOne(entity)

	if err != nil {
		return 0, err
	}

	if !ok {
		return 0, errors.New("数据不存在")
	}

	entity.Title = req.Title
	entity.Status = req.Status
	//entity.UpdateTime = time.Now()
	//entity.UpdateBy = ""

	//user := system.UserService.GetProfile(ctx)

	//if user == nil {
	//	entity.UpdateBy = user.LoginName
	//}

	return tag.TagDao.Update(entity)
}

//根据条件查询数据
func (s *tagService) SelectListAll(params *tagModel.SelectPageReq) ([]tagModel.Entity, error) {
	return tag.TagDao.SelectListAll(params)
}

//根据条件分页查询数据
func (s *tagService) SelectListByPage(params *tagModel.SelectPageReq) ([]tagModel.Entity, *page.Paging, error) {
	return tag.TagDao.SelectListByPage(params)
}

// 导出excel
func (s *tagService) Export(param *tagModel.SelectPageReq) (string, error) {
	head := []string{"", "标题", "0未审核 1正常状态 2审核未通过 3删除", "创建时间"}
	col := []string{"id", "title", "status", "create_time"}
	return tag.TagDao.SelectListExport(param, head, col)
}

//检查tag是否存在
func (s *tagService) GetByName(name string) (*tagModel.Entity, error) {

	entity := &tagModel.Entity{Title: name}
	ok, err := tag.TagDao.FindOne(entity)
	if err != nil {
		return nil, err
	}
	if !ok {
		return nil, errors.New("数据不存在")
	}

	return entity, nil
}

func (s *tagService) GetOrCreate(title string) (*tagModel.Entity, error) {

	if len(title) == 0 {
		return nil, errors.New("标签为空")
	}
	entity, _ := s.GetByName(title)
	if entity != nil {
		return entity, nil
	} else {
		entity := &tagModel.Entity{
			Title:  title,
			Status: constants.StatusOk,
			//CreateTime: time.Now(),
		}
		_, err := tag.TagDao.Insert(entity)
		if err != nil {
			return nil, err
		}

		return entity, nil
	}
}

func (s *tagService) GetOrCreates(tags []string) []int64 {
	//db := db.Instance().Engine()
	var tagIds []int64

	for _, tagName := range tags {
		tagName = strings.TrimSpace(tagName)
		tag, err := s.GetOrCreate(tagName)
		if err == nil {
			tagIds = append(tagIds, tag.Id)
		}
	}
	return tagIds
}
